package com.wingscode.modules.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import com.wingscode.common.utils.PageUtils;
import com.wingscode.common.utils.R;
import com.wingscode.modules.app.form.ChatForm;
import com.wingscode.modules.common.entity.*;
import com.wingscode.modules.common.service.*;
import com.wingscode.modules.sys.controller.AbstractController;
import com.wingscode.modules.sys.entity.InquiryEntity;
import com.wingscode.modules.sys.entity.OfferPriceEntity;
import com.wingscode.modules.sys.service.InquiryService;
import com.wingscode.modules.sys.service.OfferPriceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 增值服务
 *
 * @author yan
 */
@RestController
@RequestMapping("/app/3")
@Api("增值服务")
public class AppController3 extends AbstractController {
    @Autowired
    private ServiceUsageRecordService serviceUsageRecordService;
    @Autowired
    private UserService userService;
    @Autowired
    private AgentService agentService;
    @Autowired
    private AddedServiceService addedServiceService;
    @Autowired
    private AddedSettingService addedSettingService;
    @Autowired
    private AddedSettingPriceService addedSettingPriceService;
    @Autowired
    private UserOrderService userOrderService;

    @PostMapping("/usageRecord")
    @ApiOperation("使用记录")
    public R usageRecord(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("userId"))
            return R.error("userId为空");
        if (!params.containsKey("agentId"))
            return R.error("agentId为空");

        Integer userId = Integer.parseInt(params.get("userId").toString());

        // 员工看自己的使用记录，管理员看整个公司的使用记录
        UserEntity user = userService.getById(userId);
        if (user != null) {
            if (user.getRole() == 99)
                params.remove("userId");
        }

        PageUtils page = serviceUsageRecordService.getRecord(params);

        return R.ok(200, page);
    }

    @GetMapping("/getCount")
    @ApiOperation("使用记录累计")
    public R getCount(@RequestParam Integer agentId) {
        if (agentId == null)
            return R.error("agentId为空");

        Map<String, Object> map = new HashMap<>();

        int surplusCount = 0;
        int usageCount = serviceUsageRecordService.count(
                new QueryWrapper<ServiceUsageRecordEntity>()
                        .eq("agent_id", agentId)
                        .eq("type", 0)
        );
        int returnCount =  serviceUsageRecordService.count(
                new QueryWrapper<ServiceUsageRecordEntity>()
                        .eq("agent_id", agentId)
                        .eq("type", 1)
        );

        AgentEntity agent = agentService.getById(agentId);
        if (agent != null)
            surplusCount = agent.getQualNum() + agent.getPersonnelNum();

        map.put("usageCount", usageCount);
        map.put("returnCount", returnCount);
        map.put("surplusCount", surplusCount);

        return R.ok(200, map);
    }

    @GetMapping("/addedList")
    @ApiOperation("增值服务")
    public R addedList() {

        List<AddedServiceEntity> list = addedServiceService.list(
                new QueryWrapper<AddedServiceEntity>()
                        .eq("state", 1)
                        .orderByAsc("sort")
                        .orderByDesc("id")
        );

        return R.ok(200, list);
    }

    @GetMapping("/addedInfo")
    @ApiOperation("增值服务详情")
    public R addedInfo(@RequestParam Integer id, @RequestParam Integer agentId) {
        if (id == null)
            return R.error("id为空");
        if (agentId == null)
            return R.error("agentId为空");

        // 获得当前企业信息
        AgentEntity agent = agentService.getById(agentId);
        // 获得增值服务详情
        AddedServiceEntity entity = addedServiceService.getById(id);
        if (entity != null) {
            List<AddedSettingEntity> list = addedSettingService.list(
                    new QueryWrapper<AddedSettingEntity>()
                            .eq("service_id", id)
            );

            if (list != null && list.size() > 0) {
                for (AddedSettingEntity setting : list) {
                    if (agent.getLevel() != null) {
                        List<AddedSettingPriceEntity> priceEntityList = addedSettingPriceService.list(
                                new QueryWrapper<AddedSettingPriceEntity>()
                                        .eq("service_id", id)
                                        .eq("vip_level_id", agent.getLevel())
                        );
                        if (priceEntityList != null && priceEntityList.size() > 0)
                            setting.setPrice(priceEntityList.get(0).getPrice());
                    }
                }
            }

            entity.setSettingList(list);
        }

        return R.ok(200, entity);
    }

    @PostMapping("/orderList")
    @ApiOperation("我的订单")
    public R orderList(@RequestBody Map<String, Object> params) {
        if (!params.containsKey("userId"))
            return R.error("userId为空");
        if (!params.containsKey("agentId"))
            return R.error("agentId为空");

        Integer userId = Integer.parseInt(params.get("userId").toString());

        // 员工看自己的使用记录，管理员看整个公司的使用记录
        UserEntity user = userService.getById(userId);
        if (user != null) {
            if (user.getRole() == 99)
                params.remove("userId");
        }

        PageUtils page = userOrderService.orderList(params);

        return R.ok(200, page);
    }

//    @PostMapping("/saveOrder")
//    @ApiOperation("下单")
//    public R saveOrder(@RequestBody UserOrderEntity entity) {
//        if (entity.getUserId() == null)
//            return R.error("userId为空");
//        if (entity.getAgentId() == null)
//            return R.error("agentId为空");
//        if (entity.getPrice() == null)
//            return R.error("price为空");
//        if (entity.getAddedSettingId() == null)
//            return R.error("addedSettingId为空");
//
//
//    }

}
